Appendix K — Assignment 6
K.1 (1) Which of the following statement is False?
Python
does not have private attributes. Instead, you use naming conventions to design classes that encourage correct use.Each new class you create can provide an
__init__
method that specifies how to initialize an object’s data attributes.We can access the attribute of the class using the dot notation.
If we access an attribute starting with
_
, an error will occur.
Ans: Double click to answer the question
- Actually, by convention, Python programmers know that any attribute name beginning with an underscore (_) is for a class’s internal use only. Even when we use this convention, attributes are always accessible.
K.2 (2) Which of the following term describes the “has a” relationship between two classes?
Composition
Inheritance
Polymorphism
Encapsulation
Ans: Double click to answer the question
K.3 (3) Which of the following statement is False?
The
self
is used to represent the instance of the class inPython
; if it is included in the method definition, it must come first before any other parameters.When calling an instance method in
Python
,self
must be passed explicitly and it must come first before any other arguments.Any variable prefixed with
self
is available to every method in the class.A class definition begins with the keyword class followed by the class’s name and a colon (:).
Ans: Double click to answer the question
- We do not have to pass it, it will be passed implicitly.
K.4 (4) When we call the print()
function and use an object as the argument, which special method will be called first?
__add__
__repr__
__str__
__init__
Ans: Double click to answer the question
K.5 (5) Which of the following function can be used to check whether an object belongs to a specific class or not?
type()
isinstance()
dir()
id()
Ans: Double click to answer the question
K.6 (6) Considering the following code cell. Try to modify the integer
class so that when calling the show()
function, it will not show the decimal point and the decimal part.
K.7 (7) Modify the above classes so that we can add the objects from the integer
and the objects from the floating
class together.
K.8 (8) You may notice that the above calculation is not correct in the math sense, why? Try to import a built-in class in Python
so that the numbers can be represented exactly. In addition, calculate 3 + 3.53
exactly using the class.
Hint: You may use Copilot to give you suggestion
Since floating point representation is not exact, so there are some rounding errors.
K.9 (9) Design a triangle class that has two attributes, base
and height
and satisfies the following properties:
- Create a constructor that accepts
base
andheight
from the user. - Using the naming convention to declare the two attributes as private data so that an error occurs when the user directly accesses these two attributes using dot notation.
- Add getter (
get_base_height()
) and setter(set_base_height
) to the class so that the two attributes can be set and get simultaneously via these methods. - Create a method
area()
for the class that returns the area of the triangle. - When the user prints out the triangle object, show the
base
andheight
of the triangle.
class trinagle:
def __init__(self, base, height):
self.__base = base
self.__height = height
def get_base_height(self):
return self.__base, self.__height
def set_base_height(self, base, height):
self.__height = height
self.__base = base
def area(self):
return self.__base * self.__height / 2
def __str__(self):
return f'Triangle: base = {self.__base}, height = {self.__height}'
K.10 (10) Design a RightTriangle
class by inheriting from the above class and modifying the class so that only valid base
and height
can be set. Otherwise, you should raise an exception.
Hint: You can raise an exception using the statement raise ValueError('Invalid base and height')
class RightTriangle(trinagle):
def __init__(self, base, height):
self.set_base_height(base, height)
def set_base_height(self, base, height):
if self.valid(base, height):
self.__base = base
self.__height = height
else:
raise ValueError('Invalid base and height')
def valid(self, base, height):
# The first condition is optional
if ((base**2 + height**2)**0.5 == int((base**2 + height**2)**0.5)) and (base > 0 or height > 0):
return True
else:
return False